<?php
class AEmbed extends CActiveRecord{
    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }


    // goi den bang can ket noi   
    public function tableName() {                  
        return 'embed_data';
    }
    public function getDataById($id){
        $sql = "SELECT * FROM c_embed_data WHERE id = ".intval($id);
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $data = $command->queryRow();
        return $data;
    }
    public function getDataByEmbedId($embed_id){
        $sql = "SELECT * FROM c_embed_data_detail WHERE embed_id = ".intval($embed_id);
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $data = $command->queryAll();
        return $data;
    }
    public function getDataByAdmin($page,$row_per_page){
        $connect = Yii::app()->db;
        $sql = "SELECT count(id) as count FROM c_embed_data WHERE user_id = 0";
        $command = $connect->createCommand($sql);
        $data_count = $command->queryRow();
        $max_page = ceil($data_count["count"]/$row_per_page);
        $first = ($page-1)*$row_per_page;
        $sql = "SELECT * FROM c_embed_data WHERE user_id = 0 ORDER BY id DESC LIMIT ".$first.",".$row_per_page." ";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $data = $command->queryAll();
        return array($max_page,$data_count["count"],$data);
    }
    public function getDataByEmbedIds($embedIds){
        $data = array();
        $str_embed = implode(",",$embedIds);
        if($str_embed !=""){
            $sql = "SELECT dataId,embed_id FROM c_embed_data_detail WHERE embed_id IN (".$str_embed.")";
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $rows = $command->queryAll();
            foreach($rows as $key=>$value){
                $data[$value["embed_id"]]["data"][] = AGame::getDataById($value["dataId"]); 
            }
        } 
        return $data;
    }
}
